using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Security.Cryptography;
using PlugNT.Safe;
using PlugNT.Database.Common;
using PlugNT.Database.Common.Simple;
using PlugNT.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.DAL
{
    public class Tables
    {

        internal static string currTableName = WebConfig.TablePrefix + "tables";



        public TablesInfo GetTablesInfo(DataRow dr)
        {
            TablesInfo model = new TablesInfo();

            model.id = (int)dr["id"];
            model.postfix = dr["postfix"].ToString();
            model.dicpath = dr["dicpath"].ToString();
            model.description = dr["description"].ToString();
            model.bindclslist = dr["bindclslist"].ToString();
            model.crecomment = (bool)dr["crecomment"];
            return model;
        }







        public TablesInfo GetTablesInfo(int id)
        {
            TablesInfo model = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 * from " + currTableName);
            strSql.Append(" where id=" + id.ToString());
            DataTable dt = DbHelper.TabQuery(strSql.ToString());
            if (dt.Rows.Count > 0)
            {
                model = GetTablesInfo(dt.Rows[0]);
            }
            return model;
        }






        public TablesInfo GetTablesInfoByPostfix(string postfix)
        {
            TablesInfo model = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 * from " + currTableName);
            strSql.Append(" where postfix='" + postfix +"'");
            DataTable dt = DbHelper.TabQuery(strSql.ToString());
            if (dt.Rows.Count > 0)
            {
                model = GetTablesInfo(dt.Rows[0]);
            }
            return model;
        }





        public TablesInfo GetAccessTablesInfo()
        {
            TablesInfo model = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 * from " + currTableName);
            DataTable dt = DbHelper.TabQuery(strSql.ToString());
            if (dt.Rows.Count > 0)
            {
                model = GetTablesInfo(dt.Rows[0]);
            }
            return model;
        }


        internal static string GetBindclslist(string postfix)
        {
            string strSql = "select top 1 [bindclslist] from  [" + currTableName + "] where  postfix='" + postfix + "'";
            object obj = DbHelper.GetSingle(strSql);
            return obj != null ? obj.ToString() : "";
        }









        public bool Add(TablesInfo model)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName + "(");
            strSql.Append("[postfix],[dicpath],[description],[bindclslist],[crecomment]");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.postfix) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.dicpath) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.description) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.bindclslist) + "',");
            strSql.Append(" " + (model.crecomment ? "1" : "0"));
            strSql.Append(")");



            return DbHelper.ExecuteSql(strSql.ToString()) > 0;


        }






        public bool Update(TablesInfo model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("[dicpath]='" + PlugNT.Safe.StringHelper.SqlFilter(model.dicpath) + "',");
            strSql.Append("[description]='" + PlugNT.Safe.StringHelper.SqlFilter(model.description) + "',");
            strSql.Append("[bindclslist]='" + PlugNT.Safe.StringHelper.SqlFilter(model.bindclslist) + "',");
            strSql.Append("[crecomment]=" + (model.crecomment ? "1" : "0"));
            strSql.Append(" where [id]=" + model.id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }









        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + currTableName);
            strSql.Append(" where id=" + id.ToString() + " and postfix<>''");



            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }











        public DataTable GetList(string andWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from  [" + currTableName + "] where 1=1 ");
            strSql.Append(andWhere);

            return DbHelper.TabQuery(strSql.ToString());
        }

        public DataTable GetDicpathList(string andWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [postfix],[dicpath] from  [" + currTableName + "] where 1=1 ");
            strSql.Append(andWhere);

            return DbHelper.TabQuery(strSql.ToString());
        }




        public object GetSingle(string columnName, string andWhere)
        {
            string sql = "select top 1 " + columnName + " from [" + currTableName + "] where 1=1 " + andWhere;
            return DbHelper.GetSingle(sql);
        }

        public int GetCount(string where)
        {
            string sql = "select count(1) from [" + currTableName + "] where 1=1 " + where;
            return (int)DbHelper.GetSingle(sql);
        }




        public string Help()
        {
            return @"mailto:354990393@qq.com";
        }


    }
}
